﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace GeoSketch2.Core
{
    /// <summary>
    /// The base component for the toolbar buttons of geosketch
    /// </summary>
    public partial class BaseToolbarButton : Button
    {
        #region Fields
        private BaseGeoDisplayBox _DisplayBox;
        Point[] LeftClickPoints;//the points to draw the half rectangle to display what tool is assigned to what button
        Point[] RightClickPoints;
        
        /// <summary>
        /// True if the button of the tool is the left one
        /// </summary>
        public bool LeftButtonClicked;

        /// <summary>
        /// True if the button of the tool is the right one
        /// </summary>
        public bool RightButtonClicked;
        ToolTip Tooltip;
        #endregion

        #region Constructor
        /// <summary>
        /// The base component for the toolbar buttons of geosketch
        /// shold not be instanciated directly
        /// </summary>
        public BaseToolbarButton()
        {
            InitializeComponent();

            LeftClickPoints = new Point[]{
                new Point(this.ClientRectangle.Width/2,this.ClientRectangle.Y),
                new Point(this.ClientRectangle.X,this.ClientRectangle.Y),
                new Point(this.ClientRectangle.X,this.ClientRectangle.Height-1),
                new Point(this.ClientRectangle.Width/2,this.ClientRectangle.Height-1)
            };
            RightClickPoints = new Point[]{
                new Point(this.ClientRectangle.Width/2,this.ClientRectangle.Y),
                new Point(this.ClientRectangle.Width-1,this.ClientRectangle.Y),
                new Point(this.ClientRectangle.Width-1,this.ClientRectangle.Height-1),
                new Point(this.ClientRectangle.Width/2,this.ClientRectangle.Height-1)
            };
            LeftButtonClicked = false;
            RightButtonClicked = false;

            this.MouseUp += new MouseEventHandler(OnMouseClick);
        } 
        #endregion

        #region Events
        /// <summary>
        /// The mouse click event on a button, must be overriden
        /// </summary>
        protected virtual void OnMouseClick(object sender, MouseEventArgs e)
        {
            SetTool(Tool, sender, e);
            DisplayBox.Refresh();
        } 
        #endregion

        #region Methods
        //Draw the border of the button to tell that a tool is affected
        void DrawButtonBorder(Graphics g)
        {
            if (LeftButtonClicked)
            {
                g.DrawLines(Vars.Setup.Toolbars.ToolbarButtons.LeftClickPen, LeftClickPoints);
            }
            if (RightButtonClicked)
            {
                g.DrawLines(Vars.Setup.Toolbars.ToolbarButtons.RightClickPen, RightClickPoints);
            }
        }

        /// <summary>
        /// Assign the left clic to the tool
        /// </summary>
        protected virtual void SetLeftClickTool(BaseTool Tool)
        {
            if (ParentForm.StatusStripLeftClicToolDropDownButton != null)
            {
                ParentForm.StatusStripLeftClicToolDropDownButton.Text = Tool.Name;
                ParentForm.StatusStripLeftClicToolTextBox.Tag = Tool;
                Tool.StatusStripDropDownButton = ParentForm.StatusStripLeftClicToolDropDownButton;
                Tool.StatusStripLabelMessages = ParentForm.StatusStripLabel;
            }

            _DisplayBox.LeftClicTool.Release();
            _DisplayBox.LeftClicTool = Tool;
            Tool.SendFirstMessage();
        }

        /// <summary>
        /// Assign the right clic to the tool
        /// </summary>
        protected void SetRightClickTool(BaseTool Tool)
        {
            if (ParentForm.StatusStripRightClicToolDropDownButton != null)
            {
                ParentForm.StatusStripRightClicToolDropDownButton.Text = Tool.Name;
                ParentForm.StatusStripRightClicToolTextBox.Tag = Tool;
                Tool.StatusStripDropDownButton = ParentForm.StatusStripRightClicToolDropDownButton;
                Tool.StatusStripLabelMessages = ParentForm.StatusStripLabel;
            }

            _DisplayBox.RightClicTool.Release();
            _DisplayBox.RightClicTool = Tool;
            Tool.SendFirstMessage();
        }
        
        /// <summary>
        /// The event when the button redraws itself
        /// </summary>
        protected override void OnPaint(PaintEventArgs pevent)
        {
            base.OnPaint(pevent);
            DrawButtonBorder(pevent.Graphics);
        }

        /// <summary>
        /// Sets the tool according to the mouse button pressed
        /// </summary>
        protected void SetTool(BaseTool Tool, object sender, MouseEventArgs e)
        {
            Tool.ToolbarButton = this;
            if (e.Button == MouseButtons.Left)
            {
                SetLeftClickTool(Tool);
                foreach (Control toolbar in this.Parent.Parent.Controls)
                {
                    if (toolbar.GetType().BaseType == typeof(BaseToolbar))
                    {
                        foreach (Control control in toolbar.Controls)
                        {
                            if (control.GetType().BaseType == typeof(BaseToolbarButton) || control.GetType().BaseType.BaseType == typeof(BaseToolbarButton))//ResSet every other button
                            {
                                if (control != this)
                                {
                                    ((BaseToolbarButton)control).LeftButtonClicked = false;//For drawing purpose 
                                }
                                control.Invalidate();
                            }
                        }
                    }
                }
                LeftButtonClicked = !LeftButtonClicked;
                if (!LeftButtonClicked)
                {
                    SetLeftClickTool(new TNone());//If clicked a second time, deactivates the tool
                }
            }
            else if (e.Button == MouseButtons.Right)
            {
                SetRightClickTool(Tool);
                foreach (Control toolbar in this.Parent.Parent.Controls)
                {
                    if (toolbar.GetType().BaseType == typeof(BaseToolbar))
                    {
                        foreach (Control control in toolbar.Controls)
                        {
                            if (control.GetType().BaseType == typeof(BaseToolbarButton)||control.GetType().BaseType.BaseType == typeof(BaseToolbarButton))
                            {
                                if (control != this)
                                {
                                    ((BaseToolbarButton)control).RightButtonClicked = false;
                                }
                                control.Invalidate();
                            }
                        }
                    }
                }
                RightButtonClicked = !RightButtonClicked;
                if (!RightButtonClicked)
                {
                    SetRightClickTool(new TNone());
                }
            }
        }
        #endregion

        #region Properties
        /// <summary>
        /// Gets or sets the displaybox on which the tool will act
        /// </summary>
        public BaseGeoDisplayBox DisplayBox
        {
            get
            {
                return _DisplayBox;
            }
            set
            {
                _DisplayBox = value;
            }
        }

        /// <summary>
        /// Sets the ToolTip text of the button
        /// </summary>
        protected String ToolTipText
        {
            set
            {
                Tooltip = new ToolTip();
                Tooltip.SetToolTip(this, value);
            }
        }

        /// <summary>
        /// Gets the parent form of the button
        /// </summary>
        protected GeoSketchForm ParentForm
        {
            get
            {
                return ((GeoSketchForm)_DisplayBox.Parent);
            }
        }

        /// <summary>
        /// Gets the tool assigned to the button
        /// </summary>
        protected virtual BaseTool Tool
        {
            get
            {
                return new TNone();
            }
        }
        #endregion
    }
}
